<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <BasicForm
        :showAdvancedButton="false"
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
      >
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row: ListData) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
      >
      </BasicTable>
    </n-card>
    <!-- 新增弹窗 -->
    <basicModal @register="modalRegister" ref="modalRef" @on-ok="okModal">
      <template #default>
        <BasicForm @register="registerModal" @reset="handleReset" class="basicForm">
          <template #statusSlot="{ model, field }">
            <n-input v-model:value="model[field]" />
          </template>
        </BasicForm>
      </template>
    </basicModal>
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { basicModal, useModal } from '@/components/Modal';
  import { PlusOutlined } from '@vicons/antd';
  import { useRouter } from 'vue-router';
  import { type FormRules } from 'naive-ui';
  import { exData } from '@/utils/exData';
  import func from 'vue-temp/vue-editor-bridge';

  const props = defineProps({
    loadDataTable: Function, //子接收数据
  });
  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'NInput',
      label: '',
      componentProps: {
        placeholder: '请输入',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];
  const columns: BasicColumn<ListData>[] = [
    {
      title: '灭火器编码',
      key: 'encode',
      width: 160,
    },
    {
      title: '单位名称',
      key: 'unit_name',
      width: 160,
    },
    {
      title: '放置点',
      key: 'site',
      width: 160,
    },
    {
      title: '生产日期',
      key: 'date',
      width: 160,
    },
    {
      title: '维修日期',
      key: 'repair_time',
      width: 160,
    },
    {
      title: '报废日期',
      key: 'scrap_time',
      width: 160,
    },
  ];
  const router = useRouter();
  const formRef: any = ref(null);
  const actionRef = ref();
  const formBtnLoading = ref(false);
  const formParams = reactive({
    name: '',
    role: null,
    droit: true,
  });
  const schemasModal: FormSchema[] = [
    {
      field: 'name',
      component: 'NInput',
      label: '灭火器编码：',
      giProps: {
        span: 1,
      },
      componentProps: {
        placeholder: '',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'name',
      component: 'NInput',
      label: '单位名称',
      giProps: {
        span: 1,
      },
      componentProps: {
        placeholder: '',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'name',
      component: 'NInput',
      label: '放置点',
      giProps: {
        span: 1,
      },
      componentProps: {
        placeholder: '',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'name',
      component: 'NDatePicker',
      label: '生产日期',
      defaultValue: [1183135260000, Date.now()],
      componentProps: {
        type: 'daterange',
      },
    },
    {
      field: 'name',
      component: 'NDatePicker',
      label: '维修日期',
      componentProps: {
        type: 'daterange',
      },
    },
    {
      field: 'name',
      component: 'NDatePicker',
      label: '报废日期',
      componentProps: {
        type: 'daterange',
      },
    },
  ];
  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          // {
          //   label: '编辑',
          //   onClick: showModal.bind(null, record),
          // },
          // {
          //   label: '删除',
          //   onClick: handleShowDetail.bind(null, record),
          // },
        ],
      });
    },
  });
  const [modalRegister, { openModal, closeModal }] = useModal({
    title: '编辑',
  });
  const [registerModal, { submit }] = useForm({
    gridProps: { cols: 1 },
    collapsedRows: 3,
    labelWidth: 120,
    isFull: false,
    layout: 'horizontal',
    submitButtonText: '提交预约',
    showActionButtonGroup: false,
    schemas: schemasModal,
  });
  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  function handleShowDetail() {
    showModal.value = true;
  }
  // 滑块切换添加权限状态
  function handleChange(value: boolean) {
    console.log(value);
  }
  const emit = defineEmits<{
    (e: 'on-click', name: string): void;
  }>();
  function handleSubmit(values: Recordable) {
    emit('on-click', values);
    reloadTable();
  }
  // const loadDataTable = async (res) => {
  //   return await getTableList({ ...getFieldsValue(), ...res });
  // };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function confirmForm(e) {
    e.preventDefault();
    formBtnLoading.value = true;
    formRef.value.validate((errors) => {
      if (!errors) {
        window['$message'].success('新建成功');
        setTimeout(() => {
          showModal.value = false;
          reloadTable();
        });
      } else {
        window['$message'].error('请填写完整信息');
      }
      formBtnLoading.value = false;
    });
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
  async function okModal() {
    closeModal();
  }

  function showModal() {
    openModal();
  }
</script>

<style lang="less" scoped></style>
